#include "app_common.h"
#include "osal_types.h"
#include "osal.h"
#include "securec.h"


#if 0
app_log_level g_app_log_level[APP_LOG_MODULE_MAX] = { APP_INFO }; 

char * app_log_level_enmu2str(int level)
{
	static char *level_str[APP_LEVEL_MAX] = 
			{"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
	if(level > APP_LEVEL_MAX) {
		return NULL;
	}
	return level_str[level];
}


static char g_app_print_buf[APP_PRINT_BUF+32+1];
void app_log(app_log_module module, app_log_level level, const char *format, ...)
{
    int ret = -1;
    va_list argList;
	static osal_mutex_t mutexPrint = cn_mutex_invalid;
	if(mutexPrint == cn_mutex_invalid) osal_mutex_create(&mutexPrint);
	if(module >= APP_LOG_MODULE_MAX) {
		module = APP_LOG_ALL;
	}
	if(level >= APP_FATAL) {
		level = APP_ERR;
	}

    if(level < g_app_log_level[module] && level < g_app_log_level[APP_LOG_ALL]) {
		return;
    }
	
	(void)osal_mutex_lock(mutexPrint);
    (void)memset_s(g_app_print_buf, sizeof(g_app_print_buf), 0, sizeof(g_app_print_buf));

    va_start(argList, format);
    ret = vsprintf_s(g_app_print_buf, sizeof(g_app_print_buf), format, argList);
    va_end(argList);
	
    if(ret == -1) {
		printf("APP DEBUG ERROR!!!"); 
		return;
	}
    printf("[%s]%s", app_log_level_enmu2str(level),g_app_print_buf);
	(void)osal_mutex_unlock(mutexPrint);
	return;
}

void app_log_set_level(int module, int level)
{
	if(module > APP_LOG_MODULE_MAX) {
		return;
	}	
	g_app_log_level[module] = level;
	return;
}

void app_log_init_level(app_log_level level)
{
	for(int i = 0; i < APP_LOG_MODULE_MAX; i++)
	{
		g_app_log_level[i] = level;
	}
	return;
}

#endif


int app_log_module_str2enmu(char * module)
{
	static char *module_str[APP_LOG_MODULE_MAX] = 
			{"all", "cengine", "omip"};
	if(NULL == module) {
		return APP_LOG_MODULE_MAX;
	}
	for(int i = 0; i < APP_LOG_MODULE_MAX; i++)
	{
		if(0 == strcmp(module, module_str[i])) {
			return i;
		}
	}
	return APP_LOG_MODULE_MAX;
}




